//
//  KWTParaItemCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/8/19.
//

#import "KWTParaItemCell.h"
#import "DeviceChartFieldModel.h"
#import "RHInverterModel.h"

@interface KWTParaItemCell()
@property (nonatomic ,strong) NSMutableArray *titleLabArr;

@property (nonatomic ,strong) NSMutableArray *contentLabArr;

@property (nonatomic ,strong) NSMutableArray *itemViewArr;

@end
@implementation KWTParaItemCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

-(void)setSectionDataModel:(RHInverterModel *)sectionDataModel
{
    _sectionDataModel = sectionDataModel;
    for (UIView *view in self.itemViewArr) {
        view.hidden = YES;
    }
    kWeakSelf;
    [_sectionDataModel.rowKeys enumerateObjectsUsingBlock:^(NSString*  _Nonnull paraKey, NSUInteger key_idx, BOOL * _Nonnull stop) {
        //根据参数key的顺序来显示
        [weakSelf.sectionDataModel.itemArr enumerateObjectsUsingBlock:^(DeviceChartFieldModel * _Nonnull obj, NSUInteger item_idx, BOOL * _Nonnull stop) {
            UIView *itemView = [weakSelf.itemViewArr safeObjectAtIndex:item_idx];
            itemView.hidden = NO;
            if ([obj.keyCode isEqualToString:paraKey]) {
                QMUILabel *lab = [weakSelf.contentLabArr safeObjectAtIndex:key_idx];
                NSString *content = [NSDecimalNumber decimalStringWithDoubleString:obj.content nullStr:@"--"];
                lab.text = [NSString stringWithFormat:@"%@ %@",content,obj.unitName];
                if ([obj.content isEqualToString:@"fault"]) {
                    lab.text = @"NaN";
                }
                if ([self.reuseIdentifier isEqualToString:@"KWTParaItemCell_LCU"]||
                    [self.reuseIdentifier isEqualToString:@"KWTParaItemCell_DHM"]) {
                    QMUILabel *titleLab = [weakSelf.titleLabArr safeObjectAtIndex:key_idx];
                    titleLab.text = obj.keyName;
                }
                //枚举类型
                if (obj.isEnum) {
                   
                     //找出对应下标的枚举值
                     __block NSInteger index = -1;
                     [obj.enumIndexs enumerateObjectsUsingBlock:^(NSString*  _Nonnull enumIndex, NSUInteger idx, BOOL * _Nonnull stop) {
                         if ([obj.content integerValue] == enumIndex.integerValue) {
                             index = idx;
                         }
                     }];
                     //找到了就显示对应的枚举值
                     if (index != -1) {
                         lab.text = [NSString stringWithFormat:@"%@%@",obj.enumValues[index],obj.unitName];
                     }else{
                         //没找到,显示后台返回的值
                         lab.text = ValidNullData(obj.content);
                     }

                }
            }
        }];
    }];
    
    
}

-(void)setupUI
{
    self.contentLabArr = [NSMutableArray array];
    self.titleLabArr = [NSMutableArray array];
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = CFTabViewBgColor;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    NSArray *dataArr = @[ @{CellTitle:@"SOC",
                            CellContent:@"--",
                            CellIcon:@"local_cell_soc",
                            @"color":@"ffffff",
                            CellUint:@"(%)"},
                            @{CellTitle:@"SOH",
                              CellContent:@"--",
                              CellIcon:@"local_cell_soh",
                              @"color":@"ffffff",
                              CellUint:@"%"},
                            @{CellTitle:@"电压".rh_localized,
                              CellContent:@"--",
                              CellIcon:@"local_cell_vol",
                              @"color":@"ffffff",
                              CellUint:@"(V)"},
                            @{CellTitle:@"电流".rh_localized,
                              CellContent:@"--",
                              CellIcon:@"local_cell_ele",
                              @"color":@"ffffff",
                              CellUint:@"(A)"}
                            ];
    if ([self.reuseIdentifier isEqualToString:@"KWTParaItemCell_LCU"]) {
        dataArr = @[ @{CellTitle:@"--",
                                CellContent:@"--",
                                CellIcon:@"lcu_temp_data01",
                                @"color":@"ffffff",
                                CellUint:@"(%)"},
                                @{CellTitle:@"--",
                                  CellContent:@"--",
                                  CellIcon:@"lcu_temp_data02",
                                  @"color":@"ffffff",
                                  CellUint:@"%"},
                                @{CellTitle:@"--",
                                  CellContent:@"--",
                                  CellIcon:@"lcu_temp_data03",
                                  @"color":@"ffffff",
                                  CellUint:@"(V)"},
                                @{CellTitle:@"--",
                                  CellContent:@"--",
                                  CellIcon:@"lcu_temp_data04",
                                  @"color":@"ffffff",
                                  CellUint:@"(A)"}
                                ];
    }
    if ([self.reuseIdentifier isEqualToString:@"KWTParaItemCell_DHM"]) {
        dataArr = @[ @{CellTitle:@"--",
                                CellContent:@"--",
                                CellIcon:@"dehum_temp_data01",
                                @"color":@"ffffff",
                                CellUint:@"(%)"},
                                @{CellTitle:@"--",
                                  CellContent:@"--",
                                  CellIcon:@"dehum_temp_data02",
                                  @"color":@"ffffff",
                                  CellUint:@"%"},
                                @{CellTitle:@"--",
                                  CellContent:@"--",
                                  CellIcon:@"dehum_temp_data03",
                                  @"color":@"ffffff",
                                  CellUint:@"(V)"},
                                @{CellTitle:@"--",
                                  CellContent:@"--",
                                  CellIcon:@"dehum_temp_data04",
                                  @"color":@"ffffff",
                                  CellUint:@"(A)"}
                                ];
    }
    __block UIView *lastView;
    __block CGFloat itemW = (kUI_WINDOW_W-30-15)/2;
    self.itemViewArr = [NSMutableArray array];
    [dataArr enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [weakSelf itemViewWithIndex:idx title:dic[CellTitle] image:dic[CellIcon] unit:dic[CellUint]];
        [bgView addSubview:itemView];
        [weakSelf.itemViewArr addObject:itemView];
        if(idx == 0){
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.top.mas_offset(15);
                make.width.offset(itemW);
                make.height.offset(kScaleW(65));
            }];
        }else{
            if(idx%2==0){//换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(15);
                    make.top.equalTo(lastView.mas_bottom).offset(10);
                    make.size.equalTo(lastView);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right).offset(15);
                    make.centerY.equalTo(lastView);
                    make.size.equalTo(lastView);
                }];
            }
        }
                
        lastView = itemView;
    }];
    
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
        
   
}

//功率统计(装机容量,总功率,当前功率,能效)
-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title image:(NSString*)imageStr unit:(NSString*)unit
{
    UIView *bgView = kView;
    [bgView rounded:8 width:1 color:kBorderColor];
    bgView.backgroundColor = UIColor.whiteColor;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 0;
    titleLab.text = [NSString stringWithFormat:@"%@",title];
    titleLab.textColor = kColorRGBA(63, 68, 67, 0.7);
    titleLab.font = kSYSTEMFONT(13);
    [bgView addSubview:titleLab];
    [self.titleLabArr addObject:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.tag = 100 + idx;
    contentLab.text = @"--";
    contentLab.textColor = kColorRGBA(63, 68, 67, 1);
    contentLab.font = [UIFont boldSystemFontOfSize:16];
    [bgView addSubview:contentLab];
    [self.contentLabArr addObject:contentLab];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.right.mas_offset(-8);
        make.top.equalTo(titleLab.mas_bottom).offset(5);
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(8);
        make.right.mas_offset(-3);
        make.centerY.equalTo(img).offset(-3);
    }];
    
    return bgView;
}
@end
